package com.gaofans.vertx.boot.web.statics;

import com.gaofans.vertx.boot.web.config.VertxWebAutoConfig;
import com.gaofans.vertx.boot.web.route.RouterExtConfigurer;
import com.gaofans.vertx.boot.web.utils.WebConstant;
import io.vertx.ext.web.handler.StaticHandler;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;


/**
 * @author GaoFans
 */
@AutoConfigureAfter(VertxWebAutoConfig.class)
public class StaticHandlerExtConfig {

    @Bean
    @ConfigurationProperties(prefix = WebConstant.VERTX_WEB_STATIC_PREFIX)
    public StaticHandlerProperties staticHandlerProperties(){
        return new StaticHandlerProperties();
    }

    @Bean
    public RouterExtConfigurer staticHandlerExtConfigurer(StaticHandlerProperties properties){
        return router -> {
            router
            .route(properties.getPath())
            .handler(
                StaticHandler
                    .create()
                    .setWebRoot(properties.getWebRoot())
                    .setAllowRootFileSystemAccess(properties.isAllowRootFileSystemAccess())
                    .setAlwaysAsyncFS(properties.isAlwaysAsyncFs())
                    .setCachingEnabled(properties.isCacheEnabled())
                    .setDefaultContentEncoding(properties.getDefaultContentEncoding())
                    .setDirectoryListing(properties.isDirectoryListing())
                    .setCacheEntryTimeout(properties.getCacheEntryTimeout())
                    .setEnableFSTuning(properties.isEnableFsTuning())
                    .setEnableRangeSupport(properties.isRangeSupport())
                    .setHttp2PushMapping(properties.getHttp2PushMapping())
                    .setDirectoryTemplate(properties.getDirectoryTemplateResource())
                    .setMaxAgeSeconds(properties.getMaxAgeSeconds())
                    .setMaxCacheSize(properties.getMaxCacheSize())
                    .setFilesReadOnly(properties.isFilesReadOnly())
                    .setIncludeHidden(properties.isIncludeHidden())
                    .skipCompressionForMediaTypes(properties.getCompressedFileSuffixes())
                    .skipCompressionForSuffixes(properties.getCompressedFileSuffixes())
            );
        };
    }
}
